#include "TestEventString.h"
#include <QDebug>

void TestEventString::parseFixedEventDate()
{
    QCOMPARE(EventString("20110217", "YYYYMMDD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 2, 17).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("17022011", "DDMMYYYY").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 2, 17).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("17-02-2011", "DD-MM-YYYY").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 2, 17).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("170211", "DDMMYY").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 2, 17).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("19870707", "RRRRMMDD").toDateTime().toString("YYYY-MM-DD"),
             QDate(QDate::currentDate().year(), 7, 7).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("0308", "MMDD").toDateTime().toString("YYYY-MM-DD"),
             QDate(QDate::currentDate().year(), 3, 8).toString("YYYY-MM-DD"));
}

void TestEventString::parseFixedEventTime()
{
    QCOMPARE(EventString("201101031330", "YYYYMMDDHHmm").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(QDate(2011, 1, 3), QTime(13, 30)).toString("YYYY-MM-DD HH:mm:ss"));
    QCOMPARE(EventString("01031335", "MMDDHHmm").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(QDate(QDate::currentDate().year(), 1, 3), QTime(13, 35)).toString("YYYY-MM-DD HH:mm:ss"));
    QDate date(QDate::currentDate().year(), QDate::currentDate().month(), 3);
    QCOMPARE(EventString("031340", "DDHHmm").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(date, QTime(13, 40)).toString("YYYY-MM-DD HH:mm:ss"));
    QCOMPARE(EventString("1345", "HHmm").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(QDate::currentDate(), QTime(13, 45)).toString("YYYY-MM-DD HH:mm:ss"));
    QCOMPARE(EventString("0245", "hhmmP").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(QDate::currentDate(), QTime(14, 45)).toString("YYYY-MM-DD HH:mm:ss"));
    QCOMPARE(EventString("0345", "hhmmA").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(QDate::currentDate(), QTime(3, 45)).toString("YYYY-MM-DD HH:mm:ss"));
    QCOMPARE(EventString("0450", "hhmm").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(QDate::currentDate(), QTime(4, 50)).toString("YYYY-MM-DD HH:mm:ss"));
    QCOMPARE(EventString("201101030130", "YYYYMMDDhhmmP").toDateTime().toString("YYYY-MM-DD HH:mm:ss"),
             QDateTime(QDate(2011, 1, 3), QTime(13, 30)).toString("YYYY-MM-DD HH:mm:ss"));
}

void TestEventString::parseYearDay()
{
    QCOMPARE(EventString("2011256", "YYYYNNN").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 9, 13).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("256", "NNN").toDateTime().toString("YYYY-MM-DD"),
             QDate(QDate::currentDate().year(), 9, 13).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("3", "N").toDateTime().toString("YYYY-MM-DD"),
             QDate(QDate::currentDate().year(), 1, 3).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("2011005", "YYYYOOO").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 4, 29).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("005", "OOO").toDateTime().toString("YYYY-MM-DD"),
             QDate(QDate::currentDate().year(), 4, 29).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("2012005", "YYYYEEE").toDateTime().toString("YYYY-MM-DD"),
             QDate(2012, 4, 13).toString("YYYY-MM-DD"));
}

void TestEventString::parseShiftEventDate()
{
    QCOMPARE(EventString("20110123", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 1, 5).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20110113", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 1, 5).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20110823", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 8, 10).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20110247", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 2, 27).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20110557", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 5, 29).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20110567", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 5, 29).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("201105L7", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 5, 29).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20110757", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 7, 31).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("201112P6", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 12, 24).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("201112L6", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 12, 31).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("201112L6", "YYYYMMFD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 12, 24).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20111214", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 12, 1).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20111215", "YYYYMMWD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 12, 2).toString("YYYY-MM-DD"));
    QCOMPARE(EventString("20111214", "YYYYMMFD").toDateTime().toString("YYYY-MM-DD"),
             QDate(2011, 12, 8).toString("YYYY-MM-DD"));
}

void TestEventString::normalizeFixedEventDate()
{
    QCOMPARE(EventString("20110217", "YYYYMMDD").normalize(), EventString("20110217", "YYYYMMDD"));
    QCOMPARE(EventString("17022011", "DDMMYYYY").normalize(), EventString("20110217", "YYYYMMDD"));
    QCOMPARE(EventString("17-02-2011", "DD-MM-YYYY").normalize(), EventString("20110217", "YYYYMMDD"));
    QCOMPARE(EventString("170211", "DDMMYY").normalize(), EventString("110217", "YYMMDD"));
    QCOMPARE(EventString("19870707", "RRRRMMDD").normalize(), EventString("19870707", "RRRRMMDD"));
    QCOMPARE(EventString("0308", "MMDD").normalize(), EventString("0308", "MMDD"));
    QCOMPARE(EventString("0803", "DDMM").normalize(), EventString("0308", "MMDD"));
}

void TestEventString::normalizeFixedEventTime()
{
    QCOMPARE(EventString("201101031330", "YYYYMMDDHHmm").normalize(), EventString("201101031330", "YYYYMMDDHHmm"));
    QCOMPARE(EventString("133003012011", "HHmmDDMMYYYY").normalize(), EventString("201101031330", "YYYYMMDDHHmm"));
    QCOMPARE(EventString("133001032011", "HHmmMMDDYYYY").normalize(), EventString("201101031330", "YYYYMMDDHHmm"));
    QCOMPARE(EventString("301303012011", "mmHHDDMMYYYY").normalize(), EventString("201101031330", "YYYYMMDDHHmm"));
    QCOMPARE(EventString("01031335", "MMDDHHmm").normalize(), EventString("01031335", "MMDDHHmm"));
    QCOMPARE(EventString("031340", "DDHHmm").normalize(), EventString("031340", "DDHHmm"));
    QCOMPARE(EventString("1345", "HHmm").normalize(), EventString("1345", "HHmm"));
    QCOMPARE(EventString("4513", "mmHH").normalize(), EventString("1345", "HHmm"));
    QCOMPARE(EventString("0245", "hhmmP").normalize(), EventString("0245", "hhmm"));
    QCOMPARE(EventString("0345", "hhmmA").normalize(), EventString("0345", "hhmm"));
    QCOMPARE(EventString("0450", "hhmm").normalize(), EventString("0450", "hhmm"));
    QCOMPARE(EventString("201101030130", "YYYYMMDDhhmmP").normalize(), EventString("201101030130", "YYYYMMDDhhmm"));
}

void TestEventString::normalizeYearDay()
{
    QCOMPARE(EventString("2011256", "YYYYNNN").normalize(), EventString("2011256", "YYYYNNN"));
    QCOMPARE(EventString("256", "NNN").normalize(), EventString("256", "NNN"));
    QCOMPARE(EventString("3", "N").normalize(), EventString("3", "N"));
    QCOMPARE(EventString("2011005", "YYYYOOO").normalize(), EventString("2011005", "YYYYOOO"));
    QCOMPARE(EventString("005", "OOO").normalize(), EventString("005", "OOO"));
    QCOMPARE(EventString("2012005", "YYYYEEE").normalize(), EventString("2012005", "YYYYEEE"));
}

void TestEventString::normalizeShiftEventDate()
{
    QCOMPARE(EventString("20110123", "YYYYMMWD").normalize(),
             EventString("20110123", "YYYYMMWD"));
    QCOMPARE(EventString("201112L6", "YYYYMMFD").normalize(),
             EventString("201112L6", "YYYYMMFD"));
    QCOMPARE(EventString("20112301", "YYYYWDMM").normalize(),
             EventString("20110123", "YYYYMMWD"));
    QCOMPARE(EventString("L6122011", "FDMMYYYY").normalize(),
             EventString("201112L6", "YYYYMMFD"));
}
